<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Filemanager extends MY_Controller {

	public function __construct()
	{
		parent::__construct();	
		$this->layout_view = 'application_filemanager';
		//$this->page_auth->secure_page();
	}

	public function elfinder_init($auth = FALSE,$hash = FALSE)
	{
		if($this->authlink->verify_authlink($auth) && $this->hs->decrypt($hash))
		{
			function access($attr, $path, $data, $volume) 
			{
				return strpos(basename($path), '.') === 0       // if file/folder begins with '.' (dot)
					? !($attr == 'read' || $attr == 'write')    // set read+write to false, other (locked+hidden) set to true
					:  null;                                    // else elFinder decide it itself
			}

			$this->load->helper('path');
			
			$file = $this->_process_directory_permissions();

			$filepath = 'files/'.$file;
			$opts = array(
				// 'debug' => true, 
				'roots' => array(
					array( 
					'driver' => 'LocalFileSystem', 
					'path'   => set_realpath($filepath), 
					'URL'    => site_url($filepath) . '/',
					'accessControl' => 'access',
					'uploadOverwrite' => false
					) 
				)
			);
			
			$this->load->library('elfinder_lib', $opts);
			exit();
		}else{
			show_404();
		}
	}

	public function studentphotos($auth = FALSE,$hash = FALSE)
	{
		if($this->authlink->verify_authlink($auth) && $this->hs->decrypt($hash))
		{
			function access($attr, $path, $data, $volume) 
			{
				return strpos(basename($path), '.') === 0       // if file/folder begins with '.' (dot)
					? !($attr == 'read' || $attr == 'write')    // set read+write to false, other (locked+hidden) set to true
					:  null;                                    // else elFinder decide it itself
			}

			$this->load->helper('path');
			
			$filepath = 'assets/uploads/photos';

			$opts = array(
				// 'debug' => true, 
				'roots' => array(
					array( 
					'driver' => 'LocalFileSystem', 
					'path'   => set_realpath($filepath), 
					'URL'    => site_url($filepath) . '/',
					'accessControl' => 'access',
					'uploadOverwrite' => false
					) 
				)
			);
			
			$this->load->library('elfinder_lib', $opts);
			exit();
		}else{
			show_404();
		}
	}

	private function _process_directory_permissions($admin = FALSE)
	{
		$session = $this->session->all_userdata();
		
		$userid = $session['userid'];
		//do not change
		$hash = 'ldOpdleidfj*73uJriejdsldkei';

		$file = md5($userid.$hash);

		if(!is_dir('files/'.$file))
		{
			if(mkdir('files/'.$file))
			{
				return $file;
			}else{
				return NULL;
			}
		}else{
			return $file;
		}
		
	}


	public function index()
	{
		$session = $this->session->all_userdata();
		$this->view_data['name'] = ucwords(strtolower($session['e_f_name'].' '.$session['e_m_name'].' '.$session['e_l_name'])); 
		$this->view_data['usertype'] = ucwords(strtolower($session['userType']));
		$this->view_data['auth'] = $this->authlink->generate_authlink();
		$this->view_data['auth1'] = __link(time());
		
		if(in_array($session['userType'],['admin','registrar']))
		{
			$this->view_data['menu'] = TRUE;
		}
	}

	public function photos()
	{
		$this->page_auth->secure_page(['admin','registrar']);
		$this->view_data['menu'] = TRUE;
		$session = $this->session->all_userdata();
		$this->view_data['name'] = ucwords(strtolower($session['e_f_name'].' '.$session['e_m_name'].' '.$session['e_l_name'])); 
		$this->view_data['usertype'] = ucwords(strtolower($session['userType']));
		$this->view_data['auth'] = $this->authlink->generate_authlink();
		$this->view_data['auth1'] = __link(time());
	}
}

/* End of file filemanager.php */
/* Location: ./application/controllers/filemanager.php */